package com.woniuxy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.woniuxy.entity.Perms;
import com.woniuxy.entity.Roles;
import com.woniuxy.service.RolesPermsService;
import com.woniuxy.utils.Result;
import com.woniuxy.utils.ResultUtil;

@RestController
@RequestMapping("/rp")
public class RolesPermsController {
	@Autowired
	private RolesPermsService rolesPermsService;

	// @RequiresRoles(value ={"admin"},logical = Logical.OR)
	// @RequiresPermissions(value = {"student:del"},logical = Logical.AND)
	@RequestMapping("/findRolesPerms")
	public Result<Object> findRolesPerms() {
		Result result = new Result().setMessage(Result.SUCCESS);
		System.out.println(rolesPermsService.findRolesPerms());
		result.setData(rolesPermsService.findRolesPerms());

		return result;
	}

	// @RequiresRoles(value ={"admin"},logical = Logical.OR)
	// @RequiresPermissions(value = {"student:del"},logical = Logical.AND)
	@RequestMapping("/findRolesByUid")
	public Result<Object> findRolesByUid(int u_id) {
		Result result = new Result();
		List<Roles> list = rolesPermsService.findRolesByUid(u_id);
		if (list != null && !list.isEmpty()) {
			result.setStatus(Result.SUCCESS);
			result.setData(list);
		} else {
			result.setStatus(Result.ERROR);
		}

		return result;
	}
	
	// @RequiresRoles(value ={"admin"},logical = Logical.OR)
	// @RequiresPermissions(value = {"student:del"},logical = Logical.AND)
	@RequestMapping("/findPermsByRid")
	public Result<Object> findPermsByRid(int ro_id) {
		Result result = new Result();
		List<Perms> list = rolesPermsService.findPermsByRid(ro_id);
		if (list != null && !list.isEmpty()) {
			result.setStatus(Result.SUCCESS);
			result.setData(list);
		} else {
			result.setStatus(Result.ERROR);
		}
		return result;
	}

	// @RequiresRoles(value ={"admin"},logical = Logical.OR)
	// @RequiresPermissions(value = {"student:del"},logical = Logical.AND)
	@RequestMapping("/allPerms")
	public Result<Object> allPerms() {
		Result result = new Result().setStatus(Result.SUCCESS);

		result.setData(rolesPermsService.allPerms());

		return result;
	}

	// @RequiresRoles(value ={"admin"},logical = Logical.OR)
	// @RequiresPermissions(value = {"student:del"},logical = Logical.AND)
	@RequestMapping("/allRoles")
	public Result<Object> allRoles() {
		Result result = new Result().setStatus(Result.SUCCESS);

		result.setData(rolesPermsService.allRoles());

		return result;
	}

	// @RequiresRoles(value ={"admin"},logical = Logical.OR)
	// @RequiresPermissions(value = {"student:del"},logical = Logical.AND)
	@RequestMapping("/updateUserRoles")
	public Result<Object> updateUserRoles(int u_id, String ro_ids) {

		String[] idStrings = ro_ids.split(",");
		int[] idsArray = new int[idStrings.length];
		for (int j = 0; j < idStrings.length; j++) {
			idsArray[j] = Integer.parseInt(idStrings[j]);
		}
		// System.out.println(Arrays.toString(idsArray));

		ResultUtil<Object> resultUtil = new ResultUtil<>();

		Result result = new Result();
		if (rolesPermsService.updateUserRoles(u_id, idsArray)) {
			result.setStatus(Result.SUCCESS);
		} else {
			result.setStatus(Result.ERROR);
		}
		return result;
	}

	// @RequiresRoles(value ={"admin"},logical = Logical.OR)
	// @RequiresPermissions(value = {"student:del"},logical = Logical.AND)
	@RequestMapping("/updateRolesPerms")
	public Result<Object> updateRolesPerms(int ro_id, String pe_ids) {

		String[] idStrings = pe_ids.split(",");
		int[] idsArray = new int[idStrings.length];
		for (int j = 0; j < idStrings.length; j++) {
			idsArray[j] = Integer.parseInt(idStrings[j]);
		}
		// System.out.println(Arrays.toString(idsArray));

		Result result = new Result();
		if (rolesPermsService.updateRolesPerms(ro_id, idsArray)) {
			result.setStatus(Result.SUCCESS);
		} else {
			result.setStatus(Result.ERROR);
		}
		return result;
	}

}
